from django import forms
import re
from captcha.fields import CaptchaField

from apps.users.models import UserProfile


class EmailResiterform(forms.Form):
    code = forms.CharField(max_length=4)
    email = forms.EmailField(required=True)

class SendEmailform(forms.Form):
    email = forms.EmailField(required=True)

class ResetPwdForm(forms.Form):
    """
    重置密码
    """
    password1 = forms.CharField(required=True, min_length=6, max_length=20)
    password2 = forms.CharField(required=True, min_length=6, max_length=20)

class UserInfoForm(forms.ModelForm):
    mobile = forms.CharField(max_length=11, min_length=11, required=True)
    class Meta:
        model = UserProfile
        fields = ["nick_name","birthday","gender","address","mobile","email"]

    def clean_mobile(self):
        """
        验证手机号码
        :return:
        """
        mobile = self.cleaned_data["mobile"]
        regex_mobile = '^1[358]\d{9}$|^147\d{8}$|^176\d{8}$'
        p = re.compile(regex_mobile)
        if p.match(mobile):
            return mobile
        else:
            raise forms.ValidationError("手机号非法", code='mobile_invalid')

class UploadImageForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ["image"]

#登录
class LoginForm(forms.Form):
    # 是否必填，最大最小长度
    username = forms.CharField(required=True)
    password = forms.CharField(required=True, min_length=3)


class DyamicLoginForm(forms.Form):
    captcha = CaptchaField()

#注册时图形验证码
class RegisterGetForm(forms.Form):
    captcha = CaptchaField()

#注册接收数据
class RegisterPostForm(forms.Form):
    captcha = CaptchaField()
    username = forms.CharField(required=True,min_length=3)
    email = forms.EmailField(required=True)
    password = forms.CharField(required=True, min_length=6, max_length=20)


class ForgetPwdgetForm(forms.Form):
    """
    忘记密码表单
    """
    captcha = CaptchaField(error_messages={'invalid': '验证码错误'})


class ForgetPwdpostForm(forms.Form):
    """
    忘记密码表单
    """
    email = forms.EmailField(required=True)
    captcha = CaptchaField(error_messages={'invalid': '验证码错误'})


class ModifyPwdForm(forms.Form):
    """
    重置密码
    """
    password1 = forms.CharField(required=True, min_length=6, max_length=20)
    password2 = forms.CharField(required=True, min_length=6, max_length=20)

